//
//  BCCommentViewController.m
//  BCVersion
//
//  Created by 吴 崧 on 14-2-24.
//  Copyright (c) 2014年 Jungle. All rights reserved.
//

#import "BCCommentViewController.h"
#define MAX_LENGTH 140
@interface BCCommentViewController ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *submitBtn;

@end

@implementation BCCommentViewController



-(instancetype)initWithTaget:(NSNumber *)uid
{
    self = [super initWithNibName:@"BCCommentViewController" bundle:nil];
    if (self) {
        self.uid = uid;
       
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self addFunctionToEndEdit];
    self.commentTV.delegate = self;
     self.score = 0;
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)back:(id)sender {
    [self.view endEditing:YES];
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)score:(UIButton *)sender {
    [self.view endEditing:YES];
    if (self.score == 0) {
        [sender setSelected:YES];
        sender.alpha = 0.5;
        self.score = sender.tag;
    }else{
        UIButton *selectedBtn = (UIButton *)[self.view viewWithTag:self.score];
        selectedBtn.alpha = 1;
        [selectedBtn setSelected:NO];
        [sender setSelected:YES];
        sender.alpha = 0.5;
        self.score = sender.tag;
    }
}



- (IBAction)submitComment:(UIButton *)sender {
    [self.view endEditing:YES];

    __weak typeof(self)weakSel = self;
    NSString *alertString = nil;
    if (self.commentTV.text.length == 0) {
        alertString = @"请填写评论";
    }
    if (self.score == 0) {
        alertString = @"请评分";
    }
    if (alertString == nil) {
        
   
    [BCProgressHUD showWithTile:@"评论中" animated:YES];
    [BCNetworkClient comment:USER_MANAGER.auth touid:self.uid type:@"add" score:@(self.score) message:self.commentTV.text  success:^{
        [BCProgressHUD hideAnimated:YES];
        [BCProgressHUD showWithTile:@"评论成功" animated:YES];
        [BCProgressHUD hideAnimated:YES];
        [weakSel.navigationController popViewControllerAnimated:YES];
    } failure:^(NSError *error) {
        [BCProgressHUD hideWithError:error animated:YES];
            }];
             }
    else
    {
        [BCProgressHUD showWithTile:alertString animated:NO];
        [BCProgressHUD hideAnimated:YES];
    }
}






-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSString *oritext = textView.text;
    oritext = [oritext stringByReplacingCharactersInRange:range withString:text];
    if (oritext.length > 140) {
        return NO;
    }
    @autoreleasepool {
        NSString *commentLength = [[NSString alloc] initWithFormat:@"%u",MAX_LENGTH - oritext.length];
        _commentLengthLabel.text = commentLength;
    }
   
    return YES;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
